from neo4j import GraphDatabase,basic_auth
import json
from flask import Flask,request
from py2neo import Graph
# connect to neo4j
driver = GraphDatabase.driver("bolt://47.100.122.116:7687", auth=basic_auth("neo4j", "123456"))
# neo4j_instance = Graph("bolt://localhost:7687", auth=("neo4j", "password"),name="test")

# start web app service
app = Flask(__name__)

@app.route('/get_node_info',methods=['GET'])
def get_node_info():
    name = request.args.getlist("name")[0]
    with driver.session() as neo_session:
        cql = f"match (n) where n.name = '{name}' return properties(n)"
        result = neo_session.run(cql)
        result = json.dumps(result.data(),ensure_ascii=False).replace("nan",'暂无')
        return result 

@app.route('/get_relate_node',methods=['GET'])
def get_relate_node_info():
    name = request.args.getlist("name")[0]
    with driver.session() as neo_session:
        cql = f"match (n)-[r]-(m) where n.name = '{name}' return n,r,m"
        result = neo_session.run(cql)
        result = json.dumps(result.data(),ensure_ascii=False).replace("nan",'暂无')
        return result 
    
@app.route('/get_relate_node_full',methods=['GET'])
def get_relate_node_full():
    name = request.args.getlist("name")[0]
    print(name)
    with driver.session() as neo_session:
        cql = f"match (a)-[r]-(b) where a.name = '{name}' return a,labels(a) ,r,b,labels(b)"
        result = neo_session.run(cql)
        result = json.dumps(result.data(),ensure_ascii=False).replace("nan",'暂无')
        return result  
app.run(debug=True,threaded=True,port=5001)
